package com.tehui.app;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static com.tehui.app.MyApplication.WX_APPID;

//初始化第三方控件
public class InitializeService extends IntentService {
    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.tehui.INIT";

    public InitializeService() {
        super("InitializeService");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
            }
        }
    }

    private void performInit() {
        //腾讯bugly
        CrashReport.initCrashReport(this, "322b1e6970", false);

        //初始化微信
        initWeChat();

        //阿里百川
        initAlibc();

    }

    private void initWeChat() {
        IWXAPI wxapi = WXAPIFactory.createWXAPI(getApplicationContext(), WX_APPID, true);
        wxapi.registerApp(WX_APPID);
    }

    private void initAlibc() {
        //阿里百川SDK初始化
        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                // 设置是否使用同步淘客打点
                AlibcTradeSDK.setSyncForTaoke(true);
            }

            @Override
            public void onFailure(int code, String msg) {
                Logger.d(code + "---" + msg);
            }
        });
    }
}
